<template>
  <div class="components-container">
    <sweet-modal ref="dialog" class="dialog">
      <h3>{{title}}</h3>
      <div style=" height:200px; margin:30px 50px">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">冰柜编号</label>
            <div class="layui-input-inline">
              <el-input  v-model="Data.ICENO"   lay-verify="required" style="width:465px;"></el-input>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">所属区域</label>
            <div class="layui-input-inline">
              <el-select v-model="Data.ICEICAID" placeholder="请选择所属区域"  style="width:100%">
                <el-option v-for="item in iceAreas" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </div>
            <label class="layui-form-label">类别</label>
            <div class="layui-input-inline">
              <el-select v-model="Data.ICECODNAME" placeholder="请选择类别"  style="width:100%">
                <el-option v-for="item in iceTypes" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">横坐标</label>
            <div class="layui-input-inline">
              <el-input-number v-model="Data.ICEXP" :min="1"   lay-verify="required"></el-input-number>
            </div>
            <label class="layui-form-label">纵坐标</label>
            <div class="layui-input-inline">
              <el-input-number v-model="Data.ICEYP" :min="1" ></el-input-number>
            </div>
          </div>
        </div>
      </div>
      <div class="layui-form-item btnCenter">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="submit" :loading="this.$store.state.isloading">确定</el-button>
      </div>
    </sweet-modal>
    <!-- 详情 -->
    <sweet-modal ref="dialogInfo" class="dialog">
      <h3>{{title}}</h3>
      <div style=" height:200px; margin:30px 50px">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">冰柜编号</label>
            <div class="layui-input-inline">
              <el-input  v-model="Data.ICENO" style="width:465px;" readonly></el-input>
            </div>
              
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">所属区域</label>

             <div class="layui-input-inline">
              <el-input  v-model="Data.ICANAME"   style="width:100%" readonly></el-input>
            </div>
             <label class="layui-form-label">类别</label>
            <div class="layui-input-inline">
              <el-input v-model="Data.ICECODNAME"   style="width:100%" readonly></el-input>
            </div>
    
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">横坐标</label>
            <div class="layui-input-inline">
              <el-input v-model="Data.ICEXP"  readonly></el-input>
            </div>
            <label class="layui-form-label">纵坐标</label>
            <div class="layui-input-inline">
              <el-input  v-model="Data.ICEYP"  readonly></el-input>
            </div>
          </div>
        </div>
      </div>
      <div class="layui-form-item btnCenter">
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </sweet-modal>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
    @import "src/styles/common.scss";
    .btnCenter {
      text-align: center;
    }
</style>
<script>
import sweetModal from "@/components/sweetModal";
import apiBase from "@/api/apiBase.js";

const ADD = "新增";
const MODIFY = "修改";
const DELETE = "删除";
const INFO = "详情";

export default {
  name: "freezerManageMain",
  props: {
    orderType: {
      type: String,
      default() {
        return ADD;
      }
    }
  },
  data() {
    return {
      title: null,
      loading: false,
      iceAreas: [],
      iceTypes: [],
      Data: {}
    };
  },
  methods: {
    //功能操作
    open(order, id) {
      this.order = order;
      this.dataRepeat();
      switch (this.order) {
        case ADD:
          this.title = "冰柜新增";
          this.initSelectData();
          this.$refs.dialog.open();
          break;
        case MODIFY:
          this.title = "冰柜修改";
          this.initSelectData();
          this.dataInfo(id);
          this.$refs.dialog.open();
          break;
        case DELETE:
          this.dataDelete(id);
          break;
        case INFO:
          this.title = "冰柜详情";
          this.dataInfo(id);
          this.$refs.dialogInfo.open();
          break;
        default:
      }
    },
    //提交
    submit() {
      this.$store.state.isloading = true;
      switch (this.order) {
        case ADD:
          this.dataAdd();
          break;
        case MODIFY:
          this.dataModify();
          break;
        case DELETE:
          break;
        case INFO:
          this.$refs.dialogInfo.close();
          break;
        default:
      }
    },
    //获取所属区域
    getIceAreas() {
      apiBase.DoService({}, apiBase.getApiAdress().Ice.QueryIceArea, res => {
        let icaNameData = res.data.content,
          legendsIcaData = icaNameData.map(item => ({
            label: item.ICANAME,
            value: item.ICAID
          }));
        this.iceAreas = legendsIcaData;
      });
    },
    //获取冰柜类别
    getIceTypes() {
      apiBase.DoService({}, apiBase.getApiAdress().Code.GetIceType, res => {
        let iceTypeData = res.data.content,
          legendsIceData = iceTypeData.map(item => ({
            label: item.CodeName,
            value: item.CodeValue
          }));
        this.iceTypes = legendsIceData;
      });
    },
    //新增
    dataAdd() {
      let Data = JSON.parse(JSON.stringify(this.Data));
      apiBase.DoService(
        Data,
        apiBase.getApiAdress().Ice.Create,
        res => {
          this.$refs.dialog.close();
          this.$message({
            type: "success",
            message: "新增成功!"
          });
          this.loading = false;
          this.refreshData();
        },
        res => {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
      );
    },
    //修改
    dataModify() {
      let Data = JSON.parse(JSON.stringify(this.Data));
      apiBase.DoService(
        Data,
        apiBase.getApiAdress().Ice.Modify,
        res => {
          this.$refs.dialog.close();
          this.$message({
            type: "success",
            message: "修改成功!"
          });
          this.refreshData();
        },
        res => {
          this.$message.error(res.data.msg);
        }
      );
    },
    //删除
    dataDelete(id) {
      let Data = {
        ICEID: id
      };
      this.$confirm("此操作将永久删除该冰柜信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(callback => {
          apiBase.DoService(Data, apiBase.getApiAdress().Ice.Delete, res => {
            this.refreshData();
            this.$message({
              message: "删除成功！",
              type: "success"
            });
          });
        })
    },
    //详情
    dataInfo(id) {
      let Data = {
        ICEID: id
      };
      apiBase.DoService(Data, apiBase.getApiAdress().Ice.Detail, res => {
        this.Data = res.data.content;
        this.Data.ICEID = id;
      });
    },
    //数据重置
    dataRepeat() {
      this.Data = {};
    },
    refreshData() {
      this.$emit("refreshData");
    },
    closeDialog() {
      if (this.order === ADD || this.order === MODIFY)
        this.$refs.dialog.close();
      else if (this.order === INFO) this.$refs.dialogInfo.close();
    },
    //初始化下拉框数据
    initSelectData() {
      this.getIceTypes();
      this.getIceAreas();
    }
  },
  mounted() {},
  components: {
    sweetModal
  },
  created() {}
};
</script>